#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace PdfCreative.Pdf.Writer
{
    /// <summary>
    /// The PdfObjectReference class represents a reference to a PDF object. The reference consists of two identifiers, a primary and
    /// secondary. The secondary identifier is not used by objects created by the Writer and only included for compatibility with
    /// embedded PDF documents (which may use the secondary identifier).
    /// Clients should never need to create a PdfObjectReference instance themselves, and any self-created instance is likely to invalidate
    /// the PDF document created by the Writer. Instead, use the CreateObjectReference() method of the Writer to obtain a new
    /// object reference that is valid within the PDF document.
    /// </summary>
    [Serializable]
    public class PdfObjectReference : PdfValue
    {
        // Private fields
        // ==============
        #region Static instances
        private static PdfObjectReference S_EmptyReference = new PdfObjectReference();
        #endregion

        #region Private fields
        private int _id;
        private int _secondary;
        #endregion



        // Base class overrides
        // ====================
        #region PdfValue overrides
        /// <summary>
        /// Writes the list to the specified PDF value writer.
        /// </summary>
        /// <param name="writer">The writer</param>
        internal override void Write(IPdfValueWriter writer)
        {
            writer.WriteObjectReference(this);
        }
        #endregion



        // Public properties
        // =================
        #region Public properties
        /// <summary>
        /// Sets or retrieves the object reference's identifier
        /// </summary>
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        /// <summary>
        /// Sets or retrieves the object reference's secondary identifier.
        /// </summary>
        public int Secondary
        {
            get { return _secondary; }
            set { _secondary = value; }
        }

        /// <summary>
        /// Returns a value that represents an empty object reference.
        /// </summary>
        public static PdfObjectReference Empty
        {
            get { return S_EmptyReference; }
        }
        #endregion



        // Public methods
        // ==============
        #region Public methods
        /// <summary>
        /// Returns a value that indicates if the reference is empty.
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return _id == 0 && _secondary == 0;
        }
        #endregion



        // Object overrides
        // ================
        #region Object overrides
        /// <summary>
        /// Indicates if this instance equals the specified instance.
        /// </summary>
        /// <param name="obj">The instance to compare this instance with</param>
        /// <returns>A boolean that indicates that this instance equals the specified instance (true) or not.</returns>
        public override bool Equals(object obj)
        {
            if (null != obj && obj is PdfObjectReference)
                return
                    ((PdfObjectReference)obj).Id == this.Id &&
                    ((PdfObjectReference)obj).Secondary == this.Secondary;
            else
                return false;
        }

        /// <summary>
        /// Retrieves this instance's unique hash code.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return _id | (_secondary << 24);
        }

        /// <summary>
        /// Returns the string representation of this PdfObjectReference.
        /// </summary>
        /// <returns>The string representation</returns>
        public override string ToString()
        {
            return ToString(PdfObjectNotation.Reference);
        }

        /// <summary>
        /// Returns the string representation of this PdfObjectReference.
        /// </summary>
        /// <returns>The string representation</returns>
        public string ToString(PdfObjectNotation notation)
        {
            string result = null;

            switch (notation)
            {
                case PdfObjectNotation.Reference:
                    result = String.Format("{0} {1} R", _id, _secondary);
                    break;

                case PdfObjectNotation.Declaration:
                    result = String.Format("{0} {1} obj", _id, _secondary);
                    break;
            }

            return result;
        }

        #endregion



        // Public constructor
        // ==================
        #region Public constructor
        /// <summary>
        /// Constructs a new, empty PdfObjectReference instance.
        /// </summary>
        private PdfObjectReference() : this(0, 0) { }

        /// <summary>
        /// Constructs a new PdfObjectReference instance.
        /// </summary>
        /// <param name="id">The ID to use</param>
        public PdfObjectReference(int id) : this(id, 0) { }

        /// <summary>
        /// Constructs a new PdfObjectReference instance.
        /// </summary>
        /// <param name="id">The ID to use</param>
        /// <param name="secondary">The secondary ID</param>
        public PdfObjectReference(int id, int secondary)
        {
            _id = id;
            _secondary = secondary;
        }
        #endregion

    } // PdfObjectReference class
}
